<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.servlet.http.*" %>
<%@page import="java.text.DecimalFormat" %>
<%@page import="nespresso.beans.*" %>
<%@page import="org.apache.commons.lang3.StringUtils" %>
<% Commande c = (Commande)session.getAttribute("commande"); %>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>NESPRESSO</title>
<link rel="stylesheet" type="text/css" href="css/nespresso.css" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />

<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>

<script src="js/jquery.alerts.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.alerts.css" />

<script src="js/grid.locale-fr.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.js" type="text/javascript"></script>

<script src="js/jquery.blockUI.js" type="text/javascript"></script>

<style type="text/css">
select.fixe {
	font-family:"Courier New",sans-serif;
	font-weight:bold;
}
</style>
</head>
<body>
<div class="super-div">
<div id="divrond1">
	<table style="width:100%">
		<tr height="83px"><td width="50%"></td><td align="center">	
			<p class="grosblancitalique"><b>Commande N° <%=c.GetId() %></b></p>
		</td></tr>
	</table>
</div>
<div align="center"><font class="moyennoir">STATUT : </font><font class="moyenblanc"><%=c.GetStatut() %></font><br>
<% String message = (String)request.getAttribute("LigneMessage");
if(message == null) message = "";
%>
<form name="FLigne" id="FLigne" target="_self" method="post" action="Listeligne">
	<input type="hidden" id="nLigne" name="nLigne" value="NULL" />
<div class="divrondform">
<br>
<table align="center" width="1033px" border="0">
  <tr style="width:700"><td>
  	<table id="TabLigne">
    	<tr>
      		<th>Id</th>
      		<th>Destinataire</th>
      		<th>Capsule</th>
      		<th>Nombre</th>
      		<th>Montant</th>
    	</tr>
		<tbody>
     	<%	Ligne l = c.GetLigne1();
       		Base bc = new Base(); Base bu = new Base();
       		int j; int lg;
       		String totligne;
      		while(l != null) {
    			bc.GetResultat("capsules", "id", l.GetIdcapsule()); bc.Suivant();
       			bu.GetResultat("users", "id", l.GetIduser()); bu.Suivant();
       			String nomprenom = new String(bu.GetTexte("prenom") + " " + bu.GetTexte("nom"));
       			String nomcapsule = new String(bc.GetTexte("nom"));
       			String smontant = new String(); smontant = smontant.valueOf(l.GetNbcapsule());
       			totligne = new DecimalFormat("0.00").format(l.GetNbcapsule() * bc.GetReel("prix")); %>
       			<tr>
       				<td><%=l.GetId() %></td>
       				<td><%=nomprenom %></td>
       				<td><%=nomcapsule %></td>
       				<td><%=l.GetNbcapsule() %></td>
       				<td><%=totligne %></td>
       			</tr>
       			<% l = l.GetSuivante();
       		}
       		bu.Free(); bc.Free();
       		%>
		</tbody>
	</table>
	<table>
  	<tr>
		<td style="width:200px" class="moyenblanc">PORT&nbsp;:&nbsp;
		<input style="text-align:right" type="text" id="port" name="port" value="<%=new DecimalFormat("0.00").format(c.GetPort()) %>" maxlength="5" size="4" />&nbsp;&#128;
		</td>
		<td align="center" class="moyenblanc" style="width:650px">Total de la commande&nbsp;:&nbsp;<%=new DecimalFormat("0.00").format(c.GetMontant()) %>&#128;
		</td>
	</tr>
    </table>
	</td>
	<td width="155" height="500" valign="top"> 
        <p><input type="submit" id="Newligne" name="Valide" value="Nouvelle ligne" class="boutons"></p>
        <p><input type="submit" id="modif" name="Valide" value="Modifier la ligne" class="boutons"></p>
        <p><input type="submit" id="bSupprimer" name="Valide" value="Supprimer la ligne" class="boutons"></p>
        <p><input type="submit" id="Payer" name="Valide" value="Paiements" class="boutons"></p>
        <p><input type="submit" id="ChangeStatut" name="Valide" value="=> <%=c.GetStatutSuivant() %>" class="boutons"></p>
        <p><input type="submit" id="Retour" name="Valide" value="Retour" class="boutons"></p>
    </td>
</table>
</div>
</form>
<font class="moyenmarron"><b><%=message%></b></font>
</div>
</div>
<script type="text/javascript" language="javascript">

function formatcommande(cellvalue, options, rowobject) {
	var val = cellvalue;
	var nom = options.colModel.name;
	if(nom == "Montant") val = val + " €";
	return '<span class="c_hand">' + val + '</span>';
}

function restovaleur(cellvalue, options, cell) {
	return $(cell).text();
}

tableToGrid("#TabLigne", {
    width: 'auto',
    height: "500",
    hidegrid: false,
    scroll:true,
	colModel: [{name:'Id', editable:false, width:60, formatter:formatcommande, unformat:restovaleur},
	           {name:'Destinataire', editable:false, width:300, formatter:formatcommande, unformat:restovaleur},
               {name:'Capsule', editable:false, width:240, formatter:formatcommande, unformat:restovaleur},
               {name:'Nombre', editable:false, width:80, align:'right', formatter:formatcommande, unformat:restovaleur},
               {name:'Montant', editable:false, width:120, align:'right', formatter:formatcommande, unformat:restovaleur}],
    onSelectRow: function(id) {
        $("#nLigne").val($("#TabLigne").getCell(id, 'Id'));
    },
    ondblClickRow: function(id) {
    	$("#nLigne").val($("#TabLigne").getCell(id, 'Id'));
    	$("#modif").click();
    }
});

$(document).ready(function() {
	var sup = null;

	$.unblockUI();
	
	$("input#ChangeStatut").click(function() {
		$.blockUI({
			message:'<h1><img src="../images/wait24trans.gif" /> Traitement en cours...</h1>',
			css: { 
	            border: 'none', 
	            padding: '15px', 
	            backgroundColor: '#000', 
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px', 
	            opacity: .7,
	            color: '#fff' 
	        }
		});
	});
	
	$("input#bSupprimer").click(function() {
		if(sup == null)	sup = "sup"; else sup = null; // 1er passage lors du click (sup est valorisé à "sup") et 2ème passage lors de la simulation du click dans "submit"
	});
	
	$('form#FLigne').submit(function(e){ // Si bouton "bSupprimer : 1er passage avec confirmation (sup = "sup") et 2ème passage direct (car sup = null) suite à la simulation du click 
		if(sup == "sup") {
	    	e.preventDefault();
	    	nligne = $("#nLigne").val();
	    	if(nligne != "NULL") {
	    		$.alerts.okButton = "&nbsp;Oui&nbsp;";
	    		$.alerts.cancelButton = '&nbsp;Non&nbsp;'
	    		jConfirm("Confirmez-vous la suppression de la ligne ?", "confirmation", function(result){
	    			if(result) $('input#bSupprimer').click(); else sup = null;
	   			});
	    	}
	    	else sup = null;
		}
	});
});

// Griser les boutons si la commande n'est plus en préparation
if($("#ChangeStatut").val() != "=> EN COMMANDE") {
	$("#Newligne").attr("disabled", "disabled");
	$("#modif").attr("disabled", "disabled");
	$("#bSupprimer").attr("disabled", "disabled");
	$("#Enreg").attr("disabled", "disabled");
	if($("#ChangeStatut").val() == "COMMANDE CLOSE") {
		$("#ChangeStatut").attr("disabled", "disabled");
	}
} else { $("#Payer").attr("disabled", "disabled"); }

// Scroll vertical dans la grille
$("#TabLigne").closest(".ui-jqgrid-bdiv").attr("style",
$("#TabLigne").closest(".ui-jqgrid-bdiv").attr("style") + " overflow-y: scroll; ");

</script>
</body>
</html>
